<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /*
      无论是二叉树还是N叉树，层序遍历都是用队列的方式
      */
     var levelOrder = function (root) {
       if (!root) return []
       let res = []
        let queue = [root]
        while (queue.length) {
          let len = queue.length
          res.push([])
          for (let i = 0; i < len; i++) {
            let node = queue.shift()
            res[res.length - 1].push(node.val)
            //二叉树是直接node.left即可  N叉树只需要判断children.length即可
            let children = node.children || []
            for (let i = 0; i < children.length; i++) {
              queue.push(children[i])
            }
          }
        }
        return res
      }
    </script>
  </body>
</html>
